package nix.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo5 {

	@Test
	public void fun1() {

		Class<D> c = D.class;
		// Class的getAnnotation获得目标类的注解
		MyAnno8 myAnno8 = c.getAnnotation(MyAnno8.class);
		System.out.println(myAnno8.name() + ", " + myAnno8.age() + ", "
				+ myAnno8.gender());//zhangsan, 18, F
	}

	@Test
	public void fun2() throws NoSuchMethodException, SecurityException {

		Class<D> c = D.class;
		Method method = c.getMethod("fun1");
		// Method、Constructor、Field（父类AccessibleObject）的getAnnotation获得目标注解
		// 不明确的情况下，可以使用getAnnotations获得所有注解
		MyAnno8 myAnno8 = method.getAnnotation(MyAnno8.class);
		System.out.println(myAnno8.name() + ", " + myAnno8.age() + ", "
				+ myAnno8.gender());//lisi, 23, M
	}
}

@MyAnno8(name = "zhangsan", age = 18, gender = "F")
class D {
	@MyAnno8(name = "lisi", age = 23, gender = "M")
	public void fun1() {

	}
}

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnno8 {
	String name();

	int age();

	String gender();
}